package com.tourex.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tourex.R;
import com.tourex.utils.BaseUtils;

public class TitleLayout extends FrameLayout {

	private TextView txTitle;
	private LinearLayout layLeft, layRight;

	private static int menuId = Integer.MAX_VALUE / 2;

	public TitleLayout(Context context) {
		this(context, null);
	}

	public TitleLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TitleLayout(final Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		LayoutInflater.from(context).inflate(R.layout.layout_title, this);
		txTitle = (TextView) findViewById(R.id.tx_title);
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleLayout);
		String title = ta.getString(R.styleable.TitleLayout_title);
		txTitle.setText(title);
		ta.recycle();

		layLeft = (LinearLayout) findViewById(R.id.layout_left);
		layRight = (LinearLayout) findViewById(R.id.layout_right);

	}

	public void setBackDisable() {
		layLeft.setVisibility(View.GONE);
	}

	public void setTitle(int textId) {
		txTitle.setText(textId);
	}

	public void setTitle(String text) {
		txTitle.setText(text);
	}

	public View addLeftDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener) {
		View view = createDrawableMenu(context, drawableId, width, height, onMenuClickListener);
		LinearLayout.LayoutParams LP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		int left = BaseUtils.dip(getContext(), 10);
		int right = BaseUtils.dip(getContext(), 20);
		view.setPadding(left, 0, right, 0);
		view.setLayoutParams(LP);
		layLeft.addView(view);
		setIdTagForMenu(view);
		return view;
	}

	public View addRightDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener) {
		View view = createDrawableMenu(context, drawableId, width, height, onMenuClickListener);
		LinearLayout.LayoutParams LP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		int left = BaseUtils.dip(getContext(), 10);
		int right = BaseUtils.dip(getContext(), 10);
		view.setPadding(left, 0, right, 0);
		view.setLayoutParams(LP);
		layRight.addView(view);
		setIdTagForMenu(view);
		return view;
	}

	public void addRightTextMenu(Context context, int textId, int width, int height, int margins, OnClickListener onMenuClickListener) {
		Button button = createTextMenu(context, textId, width, height, margins, onMenuClickListener);
		layRight.addView(button);
	}

	public View addRightMenu(Context context, View menuView, OnClickListener onMenuClickListener) {
		menuView.setOnClickListener(onMenuClickListener);
		layRight.addView(menuView);
		return menuView;
	}

	public FrameLayout createDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener) {
		FrameLayout layF = (FrameLayout) LayoutInflater.from(getContext()).inflate(R.layout.part_view_title_drawable_menu, null);
		ImageView imageView = (ImageView) layF.findViewById(R.id.imageview);
		imageView.setBackgroundResource(drawableId);
		FrameLayout.LayoutParams btnLP = new FrameLayout.LayoutParams(BaseUtils.dip(context, width), BaseUtils.dip(context, height));
		imageView.setLayoutParams(btnLP);
		btnLP.gravity = Gravity.CENTER_VERTICAL;
		layF.setOnClickListener(onMenuClickListener);
		return layF;
	}

	public Button createTextMenu(Context context, int textId, int width, int height, int margins, OnClickListener onMenuClickListener) {
		Button button = new Button(context);
		button.setText(textId);
		button.setTextSize(16);
		button.setGravity(Gravity.CENTER);
		button.setTextColor(Color.WHITE);
		button.setBackgroundColor(Color.TRANSPARENT);

		LinearLayout.LayoutParams btnLP = new LinearLayout.LayoutParams(BaseUtils.dip(context, width), BaseUtils.dip(context, height));

		margins = BaseUtils.dip(context, margins);
		btnLP.setMargins(margins, margins, margins, margins);

		button.setLayoutParams(btnLP);

		button.setOnClickListener(onMenuClickListener);
		return button;
	}

	public void removeAllLeftMenu() {
		layLeft.removeAllViewsInLayout();
	}

	public void removeAllRightMenu() {
		layRight.removeAllViewsInLayout();
	}

	private void setIdTagForMenu(View view) {
		view.setId(menuId);
		menuId++;
	}
}
